package cn.leon.search.portal.controller;

import cn.leon.common.pojo.JsonUtils;
import cn.leon.content.service.ContentService;
import cn.leon.pojo.Content;
import cn.leon.search.portal.pojo.Ad1Node;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private ContentService contentService;

    @Value("${AD1_CONTENT_CID}")
    private Long AD1_CONTENT_CID;
    @Value("${AD1_HEIGHT}")
    private Integer AD1_HEIGHT;
    @Value("${AD1_WIDTH}")
    private Integer AD1_WIDTH;
    @Value("${AD1_HEIGHT_B}")
    private Integer AD1_HEIGHT_B;
    @Value("${AD1_WIDTH_B}")
    private Integer AD1_WIDTH_B;


    @RequestMapping("/index")
    public String showIndex(Model model){
        List<Ad1Node> ad1NodeList = new ArrayList<>();
        List<Content> contentList = contentService.getContent(AD1_CONTENT_CID);
        for (Content content : contentList) {
            Ad1Node ad1Node = new Ad1Node();
            ad1Node.setAlt(content.getTitle());
            ad1Node.setHeight(AD1_HEIGHT);
            ad1Node.setHref(content.getUrl());
            ad1Node.setHeightB(AD1_HEIGHT_B);
            ad1Node.setSrc(content.getPic());
            ad1Node.setSrcB(content.getPic2());
            ad1Node.setWidth(AD1_WIDTH);
            ad1Node.setWidthB(AD1_WIDTH_B);
            ad1NodeList.add(ad1Node);
        }
        //转换成json
        String json = JsonUtils.objectToJson(ad1NodeList);
        System.out.println(json);
        model.addAttribute("ad1",json);
        return "index";
    }
}
